<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */


/**
* @package DomAr
*/
final class DomArValidationReport {

	protected $validationInfo;
	
	private static $lastReport = NULL;
	
	private static $lastChecked = true;

	public function __construct( array $validationInfo ) {
		$this->validationInfo = $validationInfo;
		self::$lastReport = $this;
		self::$lastChecked = false;
	}
	
	/**
	 * @return DomArValidationReport
	 */
	public static function getLastReport() {
		return self::$lastReport;
	}
	
	/**
	 * Returns array where each element describes each failed validation.
	 *
	 * @return Array
	 */
	public function getInfo() {
		return $this->validationInfo;
	}
	
	/**
	 * Returns array of user messages.
	 *
	 * @return Array
	 */
	public function getMessages() {
		$messages = array();
		foreach( $this->validationInfo as $info ) {
			$messages[] = $info['userMessage'];
		}
		return $messages;
	}
	
	/**
	 * Returns last created user message.
	 *
	 * @return String
	 */
	public function getLastMessage() {
		return array_pop($this->getMessages());
	}
	
	/**
	 * Change all objects validation status to true.
	 *
	 */
	public function setValidated() {
		foreach( $this->validationInfo as $data )
			$data['object']->setValidated();
	}
	
	public function __get( $var ) {
		switch( $var ) {
			case 'messages':
				return $this->getMessages();
			break;
			case 'lastMessage':
				return $this->getLastMessage();
			break;
		}
	}
	
	/**
	 * Add $messageValues['forceMessage'] to force custom user message. Not recommended through!
	* @internal
	*/
	public static function makeValidationMessage( DomArObject $object, $errorNr, $var, $value, $userErrors = true, array $messageValues = array() ) {
		
		$error = '';
		if( $userErrors === true ) {
		
			if( isset($messageValues['forceMessage']) === false ) {
				// get error from object method errors.
				$userMessages = $object->getValidationErrorMessages();
				if( isset($userMessages[$var][$errorNr]) ) {
					$error = $userMessages[$var][$errorNr];			}
				
				// get error from global validation errors file.
				if( $error === '' && file_exists(DOM_CONF_DIR_PATH.'validationErrors.lang.php') === true ) {
					require(DOM_CONF_DIR_PATH.'validationErrors.lang.php');
					if( isset($DOMAR_VALIDATION_ERRORS[get_class($object)][$var][$errorNr]) ) {
						$error = $DOMAR_VALIDATION_ERRORS[get_class($object)][$var][$errorNr];
					}
					//} else if( isset($DOMAR_VALIDATION_ERRORS['DEFAULT'][$errorNr]) ) {
					//	$error = $DOMAR_VALIDATION_ERRORS['DEFAULT'][$errorNr];
					//}
				}
			} else {
				$error = $messageValues['forceMessage'];
			}

			if( $error !== '') {
				if( is_string($value) or is_numeric($value))
					$error = str_replace('[$value]',$value,$error);
				$error = str_replace('[$errorNr]',$errorNr,$error);
				$error = str_replace('[$property]',$var,$error);
			
				foreach( $messageValues as $replKey => $replValue )
					$error = str_replace('[$'.$replKey.']', $replValue, $error);
				
				return $error;
			}
		}
		
		if( is_object($value) )
			$value = get_class($value);
		
		else if( is_array($value) )
			$value = 'Array';

		elseif( is_null($value) )
			$value = 'NULL';

		elseif( is_int($value) )
			$value = '(int)'.$value;

		elseif( is_float($value) )
			$value = '(float)'.$value;
		
		else
			$value = strlen($value) > 80 ? substr($value,0,80).'...' : $value;

		return $errorNr.') Bad value <strong>'.$value.'</strong> for class <strong>'.get_class($object).'</strong> property <strong>'.$var.'</strong>!';
	}

	public function __destruct() {
		//if( self::$lastChecked === false && self::$lastReport == $this ) {
		//	throw new RuntimeException('Object failed validation is not checked! Validation errors were: <br>'.implode('<br>', $this->__get('messages') ).'');
		//}
	}

}
?>